package com.jibei.mendo.utils;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.ViewGroup;

/**
 * Created by Devin on 2019/8/11.
 * 旋转菜单工具类
 */
public class RotateMenuUtil {

    private RotateMenuUtil() {

    }

    public static RotateMenuUtil getInstance() {
        return RotateMenuUtil.Holder.instance;
    }

    private static class Holder {
        private static final RotateMenuUtil instance = new RotateMenuUtil();
    }

    public void hideView(ViewGroup view, Animator.AnimatorListener animatorListener) {
        hideView(view, 0, animatorListener);
    }

    public void showView(ViewGroup view, Animator.AnimatorListener animatorListener) {
        showView(view, 0, animatorListener);
    }

    /**
     * 隐藏动画
     *
     * @param view             被隐藏的动画
     * @param startOffset      延迟多久开始执行动画
     * @param animatorListener 动画状态监听
     */
    public void hideView(ViewGroup view, int startOffset, Animator.AnimatorListener animatorListener) {
        view.setPivotX(view.getWidth());
        view.setPivotY((float) view.getHeight() / 2);

        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0, 180);
        animator.setDuration(500);
        animator.setStartDelay(startOffset);
        if (animatorListener != null) {
            animator.addListener(animatorListener);
        }
        animator.start();
    }

    /**
     * 展示动画
     *
     * @param view             被展示的动画
     * @param startOffset      延迟多久开始执行动画
     * @param animatorListener 动画状态监听
     */
    public void showView(ViewGroup view, int startOffset, Animator.AnimatorListener animatorListener) {
        view.setPivotX(view.getWidth());
        view.setPivotY((float) view.getHeight() / 2);

        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 180, 360);
        animator.setDuration(500);
        animator.setStartDelay(startOffset);
        if (animatorListener != null) {
            animator.addListener(animatorListener);
        }
        animator.start();
    }
}
